﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using DBAccess;
using System.Collections.Generic;
namespace SD.Database
{
    public partial class Planets
    {
        public static void UseSupplayUnits(int sId, int pId, BuldingTypes what)
        {
            if (UserFunctions.FleetOwner(sId, pId) != 0 && UserFunctions.FleetOwner(sId, pId) != Planets.PlanetOwner(sId, pId))
                return;

            int owner = Planets.PlanetOwner(sId, pId);
            int supply = UserFunctions.GetPlayerInt(owner, "SupplyUnits");
            if (supply == 0)
                return;
            string cmd;
            switch (what)
            {
                case BuldingTypes.Farm:
                    cmd = "Update planets Set Farms = Farms + 1 Where PlanetId = @param0 and SystemId = @param1";
                    break;
                case BuldingTypes.Factory:
                    cmd = "Update planets Set Factories = Factories + 1 Where PlanetId = @param0 and SystemId = @param1";
                    break;
                case BuldingTypes.Cybernet:
                    cmd = "Update planets Set Cybernets = Cybernets + 1 Where PlanetId = @param0 and SystemId = @param1";
                    break;
                case BuldingTypes.Lab:
                    cmd = "Update planets Set Labs = Labs + 1 Where PlanetId = @param0 and SystemId = @param1";
                    break;
                default:
                    return;
            }
            DAL.GetInstance().UpdateData(cmd, new List<object> { pId, sId });
            DAL.GetInstance().UpdateData("Update players set SupplyUnits = SupplyUnits -1 where PlayerID = @param0", new List<object>() { owner });
        }
        public static string Build(int sId, int pId, BuldingTypes what, int quantity)
        {
            if (UserFunctions.FleetOwner(sId, pId) != 0 && UserFunctions.FleetOwner(sId, pId) != Planets.PlanetOwner(sId, pId))
                return string.Empty;
            string s = sId + " " + pId + " " + what.ToString() + " " + quantity.ToString() + "<br/>\n";

            bool market = UserFunctions.PlayerHasSpecialBulding(Planets.PlanetOwner(sId, pId), SpecialBuldings.Market);
            //BULDINGS part
            if (what == BuldingTypes.Farm || what == BuldingTypes.Factory || what == BuldingTypes.Cybernet || what == BuldingTypes.Lab || what == BuldingTypes.Starbase)
            {
                string cmd = "";
                while (quantity > 0)
                {
                    switch (what)
                    {
                        case BuldingTypes.Farm:
                            cmd = "Select Farms From Planets Where PlanetId = @param0 and SystemId = @param1";
                            break;
                        case BuldingTypes.Factory:
                            cmd = "Select Factories From Planets Where PlanetId = @param0 and SystemId = @param1";
                            break;
                        case BuldingTypes.Cybernet:
                            cmd = "Select Cybernets From Planets Where PlanetId = @param0 and SystemId = @param1";
                            break;
                        case BuldingTypes.Lab:
                            cmd = "Select Labs From Planets Where PlanetId = @param0 and SystemId = @param1";
                            break;
                        case BuldingTypes.Starbase:
                            cmd = "Select Starbase From Planets Where PlanetId = @param0 and SystemId = @param1";
                            break;
                    }


                    //get current level of building and the remaining pp's to the next level
                    double bulLev = DAL.GetInstance().GetDouble(cmd, new List<object>() { pId, sId });
                    cmd = "Select ProductionPoints From Planets Where PlanetId = @param0 and SystemId = @param1";
                    double pp = DAL.GetInstance().GetDouble(cmd, new List<object>() { pId, sId });

                    int ppCost = Equations.BuildingCost((int)bulLev);
                    if (market == true) ppCost = (int)(ppCost * 0.9);
                    double remainingPP = Math.Abs((1.0 - (bulLev - (int)bulLev)) * ppCost * 1.0);
                    s += bulLev.ToString() + "; " + pp.ToString() + " " + ppCost.ToString() + " " + remainingPP.ToString() + "<br/>\n";
                    //update the table
                    if (pp - remainingPP >= 0)
                    {
                        switch (what)
                        {
                            case BuldingTypes.Farm:
                                cmd = "Update planets Set Farms = Farms + 1, ProductionPoints = ProductionPoints - @param0 Where PlanetId = @param1 and SystemId = @param2";
                                break;
                            case BuldingTypes.Factory:
                                cmd = "Update planets Set Factories = Factories + 1, ProductionPoints = ProductionPoints - @param0 Where PlanetId = @param1 and SystemId = @param2";
                                break;
                            case BuldingTypes.Cybernet:
                                cmd = "Update planets Set Cybernets = Cybernets + 1, ProductionPoints = ProductionPoints - @param0 Where PlanetId = @param1 and SystemId = @param2";
                                break;
                            case BuldingTypes.Lab:
                                cmd = "Update planets Set Labs = Labs + 1, ProductionPoints = ProductionPoints - @param0 Where PlanetId = @param1 and SystemId = @param2";
                                break;
                            case BuldingTypes.Starbase:
                                cmd = "Update planets Set Starbase = Starbase + 1, ProductionPoints = ProductionPoints - @param0 Where PlanetId = @param1 and SystemId = @param2";
                                break;
                        }
                        s += cmd + "<br/>";
                        DAL.GetInstance().UpdateData(cmd, new List<object>() { remainingPP, pId, sId });

                        quantity--;
                    }
                    else
                        return s;
                }

            }
            else
            {//SHIPS part

                int cost = 0;
                int EcoBonus = UserFunctions.EcoBonus(Planets.PlanetOwner(sId, pId));
                if (what == BuldingTypes.Cruiser && UserFunctions.GetPlayerFloat(Planets.PlanetOwner(sId, pId), "Math") < 15)
                    return s;
                if (what == BuldingTypes.Battleship && UserFunctions.GetPlayerFloat(Planets.PlanetOwner(sId, pId), "Physics") < 15)
                    return s;
                switch (what)
                {
                    case BuldingTypes.CS:
                        cost = int.Parse(Configuration.GetConfigurationForOption("ColonyShipPrice")) - EcoBonus;
                        break;
                    case BuldingTypes.Transport:
                        cost = int.Parse(Configuration.GetConfigurationForOption("TransportPrice")) - EcoBonus;
                        break;
                    case BuldingTypes.Destroyer:
                        cost = int.Parse(Configuration.GetConfigurationForOption("DestroyerPrice")) - EcoBonus;
                        break;
                    case BuldingTypes.Cruiser:
                        cost = 8 * (int.Parse(Configuration.GetConfigurationForOption("DestroyerPrice")) - EcoBonus);
                        break;
                    case BuldingTypes.Battleship:
                        cost = 20 * (int.Parse(Configuration.GetConfigurationForOption("DestroyerPrice")) - EcoBonus);
                        break;
                }
                cost = cost * quantity;
                string cmd = "";
                try
                {
                    int productionPP = (int)DAL.GetInstance().GetDouble("Select ProductionPoints From Planets Where PlanetId = @param0 and SystemId = @param1", new List<object>() { pId, sId });
                    if (productionPP - cost >= 0)
                    {

                        switch (what)
                        {
                            case BuldingTypes.CS:
                                cmd = "Update planets Set ColonyShips = ColonyShips + @param0, ProductionPoints = ProductionPoints - @param1 Where PlanetId = @param2 and SystemId = @param3";
                                break;
                            case BuldingTypes.Transport:
                                cmd = "Update planets Set Transports = Transports + @param0, ProductionPoints = ProductionPoints - @param1 Where PlanetId = @param2 and SystemId = @param3";
                                break;
                            case BuldingTypes.Destroyer:
                                cmd = "Update planets Set Destroyers = Destroyers + @param0, ProductionPoints = ProductionPoints - @param1 Where PlanetId = @param2 and SystemId = @param3";
                                break;
                            case BuldingTypes.Cruiser:
                                cmd = "Update planets Set Crusiers = Crusiers + @param0, ProductionPoints = ProductionPoints - @param1 Where PlanetId = @param2 and SystemId = @param3";
                                break;
                            case BuldingTypes.Battleship:
                                cmd = "Update planets Set Battleships = Battleships + @param0, ProductionPoints = ProductionPoints - @param1 Where PlanetId = @param2 and SystemId = @param3";
                                break;
                        }
                        DAL.GetInstance().UpdateData(cmd, new List<object>() { quantity, cost, pId, sId });
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(cmd, ex);
                }
            }
            return s;
        }
    };
}